package com.tianji.jersey.resource.client;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
import com.tianji.jersey.jaxb.bean.Flights;

public class HttpResourceCaller<T> {
	private Log log = LogFactory.getLog(this.getClass());
	private String url;
	private String contextPath;
	
	public HttpResourceCaller(Class<T> clazz,String url) {
		this.url = url;
		log.debug("clazz-->"+clazz);
		contextPath = clazz.getPackage().getName();
	}

	public String get() {
		log.debug("===========T get================");
		Client client = Client.create();
		WebResource resource = client.resource(url);
//		resource.accept("application/json");
		resource.accept("application/xml").get(Flights.class);
		String str = (String) resource.get(String.class);
		return str;
//		return unmarshal(str);
	}

	public void post() {
		Client c = Client.create();
		WebResource resource = c.resource(url);
		resource.post();
	}

	public T post(Form form) {
	    Client c = Client.create();
		WebResource resource = c.resource(url);

		String str = null;
		if (form != null) {
			str = resource.post(String.class, form);
		} else {
			str = resource.post(String.class, new Form());
		}
		return unmarshal(str);
	}
	
	public T put(Form form) {
		Client c = Client.create();
		WebResource resource = c.resource(url);

		String result = null;
		if (form != null) {
			result = resource.put(String.class, form);
		} else {
			result = resource.put(String.class);
		}

		return unmarshal(result);
	}

	public void delete() {
	    Client c = Client.create();
		WebResource resource = c.resource(url);
		resource.delete();
	}

	public T delete(Form form) {
		Client c = Client.create();
		WebResource resource = c.resource(url);
		String result = null;
		if (form != null) {
			result = resource.delete(String.class, form);
		} else {
			result = resource.delete(String.class);
		}
		return unmarshal(result);
	}
	@SuppressWarnings("unchecked")
	private T unmarshal(String xmlStr) {
		if (xmlStr == null) {
			return null;
		}
	
		try {
			JAXBContext context = JAXBContext.newInstance(contextPath);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			StringReader reader = new StringReader(xmlStr);
			T object = (T)unmarshaller.unmarshal(reader);
			reader.close();
			return object;
		} catch (JAXBException e) {
			if (log.isErrorEnabled()) {
				log.error("JAXBException, " + e);
			}
		}

		return null;
	}

	public static void main(String[] args) {
//		String url = "http://192.168.1.175/jersey-test/jersey/jersey/media2";
//		HttpResourceCaller<AnswerVoList> caller = new HttpResourceCaller<AnswerVoList>(AnswerVoList.class,url);
//		AnswerVoList list = caller.get();
//		System.out.println(list.getAnswerVoList().size());
		
	}
}
